<?php
header ('Content-type: text/html; charset=utf-8');
set_time_limit(0) ;
if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}
$defaultType=isset($_GET['type'])?intval($_GET['type']):1;
unset($_GET['type']);
$select=array($defaultType=>'selected');

$op   = array('0'=>'getPaperList','1'=>'getEmotionList','6'=>'getPlannerTagsList','7'=>'getPlannerPapersList','8'=>'getPlannerStickersList','9'=>'getPlannerBrushsList','11'=>'getPlannerFramesList','12'=>'getPlannerPluginsList');
$url  = array('0'=>'admin','1'=>'admin','6'=>'planner','7'=>'planner','8'=>'planner','9'=>'planner','11'=>'planner','12'=>'planner');
$ac   = array('0'=>'shop_class','1'=>'shop_class','6'=>'planner_class','7'=>'planner_class','8'=>'planner_class','9'=>'planner_class','11'=>'planner_class','12'=>'planner_class');
if($_GET['op'] == 'submit'){
    if(empty($_FILES['package'])){
        echo '上传文件不能为空';
        exit;
    }
    //上传的参数
    $id   =intval($_POST['id']);
    $type =intval($_POST['type']);
    $subtype = intval($_POST['subtype']);
    //上传文件信息
    $packageName     = $_FILES['package']['name'];
    $packageSize     = $_FILES['package']['size'];
    $packageErrorNum = $_FILES['package']['error'];  
    $packageTmpName  = $_FILES['package']['tmp_name'];
     
    $link_url = $url[$type].'.php?ac='.$ac[$type].'&op='.$op[$type];
    if($_FILES['package']['size']>80000*1024*1024){
      showmessage('文件太大',$link_url);
    }

    if(!move_uploaded_file($packageTmpName, './tmp/'.$packageName) || $packageErrorNum > 0){      
        showmessage('上传失败',$link_url);
    }
 
    $arrtitle=explode('.',$packageName);
    //title
    $title=urlencode($arrtitle[0]);
    
    if(array_pop($arrtitle) != 'zip'){      
        showmessage('包的格式不正确，需要zip类型',$link_url);     
    }
    //name
    $name=urlencode($_POST['name']);
  
    //解压 
        $url_unzip='http://127.0.0.1:9080/unzip?title='.$title.'&id='.$id;

        $res=json_decode(getHttpResponsePOST($url_unzip),TRUE);
     
        if($res['resultData']!=1){
            showmessage('解压失败',$link_url);
        }

    //打包并上传CDN
    $url_pack='http://127.0.0.1:9080/pack?id='.$id.'&name='.$name.'&title='.$title.'&type='.$type.'&subtype='.$subtype;
  
    $res=json_decode(getHttpResponsePOST($url_pack),TRUE);

    if(empty($res['resultData'])){
        showmessage('打包失败',$link_url);
    }
    $m=array_combine($res['resultData']['m_key'],$res['resultData']['m_val']);
  
    $sid        = $res['resultData']['info']['id'];
    $title      = $res['resultData']['info']['title'];
    $name       = $res['resultData']['info']['name'];
    $priview    = JsonFormat_m($res['resultData']['s']);
    $linkinfo   = JsonFormat_m($m);
    $linkzip    = $res['resultData']['zip'];
    $cover      = $res['resultData']['cover'];

    //打包完成的数据插入数据表
    $param=array(
        'do'        =>'shop',
        'action'    =>'insertPackMsg',
        'format'    => 'json',
        'sid'       => $sid,
        'title'     => $title,
        'name'      => $name,
        'priview'   => $priview,
        'linkinfo'  => $linkinfo,
        'linkzip'   => $linkzip,
        'cover'     => $cover,
        'type'      => $type
    ) ;

    $content =json_decode(get_api_content($_SC ['apiurl'], $param),TRUE);
  
   
    if(!$content['resultData']){
      showmessage('插入数据库失败',$link_url);
    }
      
    
   showmessage('打包成功',$link_url);
   
}
